<?php
class advertisment extends CI_Controller{
	public function __construct()
    {
		parent::__construct();
		$this->load->model('advertisment_model');
	}
	function index($start = 0){
		$limit = 10;
		// get all advertisment
		$result = $this->advertisment_model->get_all_advertisment($limit,$start);
		$data['advertisments'] = $result['rows'];
		$data['all'] = $result['all'];
		// post ajax
		$_GET['action'] = (isset($_GET['action'])) ? $_GET['action'] :"";
		if($_GET['action']=='edit'){
			echo $this->advertisment_model->get_advertisment_by_id($this->input->post('advertisment_id'));		
		}else{
			// pagination
			$this->load->library('pagination');
			$config = array();
			$config['base_url'] = site_url()."/advertisment/index/";
			$config['total_rows'] = $result['totalRows'];
			$config['per_page'] = $limit;
			$config['uri_segment'] = 3;
			$config['num_links'] = 3;
	
			$this->pagination->initialize($config);
			$data['pagination'] = $this->pagination->create_links();	
			$this->load->view('advertisment',$data);
		}
	  }
	// add new advertisment
	function add(){
		//$data['adv_id']         = $this->input->post('advertisment_id');
        $data['adv_type']       = $this->input->post('advertisment_type');
		$data['adv_title']      = $this->input->post('advertisment_title');
		$data['adv_des']        = $this->input->post('advertisment_des');
        $data['adv_content']    = $this->input->post('advertisment_content');
		$data['status']         = $this->input->post('status');
        $data['ordering']       = $this->input->post('ordering');
		$this->index();
	}
	// edit advertisment
	function edit(){
		$id                     = $this->input->post('adv_id');
		$data['adv_type']       = $this->input->post('advertisment_type');
		$data['adv_title']      = $this->input->post('advertisment_title');
		$data['adv_des']        = $this->input->post('advertisment_des');
        $data['adv_content']    = $this->input->post('advertisment_content');
		$data['status']         = $this->input->post('status');
        $data['ordering']       = $this->input->post('ordering');
		$this->advertisment_model->edit_advertisment($id,$data);
		$this->index();	
	}
	// delete advertisment
	function delete(){
		$id_delete = $_POST['selected_box'];
		$this->advertisment_model->delete_advertisment($id_delete);
		$this->index();
	}
}